package work.cqhx.springapi.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import work.cqhx.springapi.mapper.StudentMapper;
import work.cqhx.springapi.pojo.Student;
import work.cqhx.springapi.returnjson.ReturnData;
import work.cqhx.springapi.returnjson.ReturnJson;

import java.util.*;

@Service
public class StudentService {
    @Autowired
    private StudentMapper studentMapper;
    public List<Student> queryStudentAll()
    {
        return studentMapper.queryStudentAll();
    }

    public List<Student> queryStudentAsc() {
        return studentMapper.queryStudentAsc();
    }
    public List<Student> queryStudentDesc() {
        return studentMapper.queryStudentDesc();
    }
    public Student queryStudentMax(){
        return studentMapper.queryStudentMax();
    }
    public Student queryStudentMin(){
        return studentMapper.queryStudentMin();
    }

    public Boolean addStudent(Student student)
    {
        return studentMapper.addStudent(student);
    }
    public Boolean deleteStudent(Integer id)
    {
        return studentMapper.deleteStudent(id);
    }
    public Boolean updateStudent(Student student)
    {
        return studentMapper.updateStudent(student);
    }

    public Integer queryStudentCount()
    {
        return studentMapper.queryStudentCount();
    }

    public Integer queryChineseQualifiedCount()
    {
        return studentMapper.queryChineseQualifiedCount();
    }

    public Integer queryMathQualifiedCount()
    {
        return studentMapper.queryMathQualifiedCount();
    }

    public Integer queryEnglishQualifiedCount()
    {
        return studentMapper.queryEnglishQualifiedCount();
    }


    public List<Object> queryClassesAll(){
        List<String> classes = studentMapper.queryClassesAll();
        Set<String> uniqueClasses = new HashSet<>(classes);
        return new ArrayList<>(uniqueClasses);
    }
    public ReturnData queryClassesAverage(){
        List<String> classes = studentMapper.queryClassesAll();
        Set<String> uniqueClasses = new HashSet<>(classes);

        List<List<Object>> data = new ArrayList<>();
        ReturnData  ReturnData = new ReturnData();
        for(String className : uniqueClasses){
            Integer average = studentMapper.queryClassesAverage(className);
            data.add(Arrays.asList(className, average));
        }
        ReturnData.setData(data);
        return ReturnData;
    }

    public List<Student> conditionalQueriesStudent(String classes, String name){
        return studentMapper.conditionalQueriesStudent(classes, name);
    }

    public Boolean deleteStudentBatch(List<Integer> ids)
    {
        return studentMapper.deleteStudentBatch(ids);
    }

}
